import axios from 'axios'
import Store from '@/utils/store'

const service = axios.create({
  baseURL: process.env.BASE_API,
  method: 'POST',
  timeout: 15000
})
const userInfo = Store.fetchLocal('loginInfoSMP')
service.interceptors.request.use(
  config => {
    config.headers.ShopID = userInfo ? userInfo.shop_id : ''
    config.headers.UserID = userInfo ? userInfo.id : ''
    config.headers.Accept = 'application/prs.live.v1.0.0+json'
    return config
  },
  error => {
    console.log(error) // for debug
    Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    if (response.status === 200 || response.status === 201 || response.status === 204) {
      response = response.data
      if (response) {
        return Promise.resolve(response)
      } else {
        return Promise.resolve(true)
      }
    } else {
      return Promise.resolve(false)
    }
  },
  error => {
    if (error.response) {
    }
  }
)

export default async function request(options) {
  let response
  try {
    response = await service(options)
    return response.data
  } catch (err) {
    return err
  }
}
